<?php

class CW_UploadClass {
	/***
	 * Destination path of the file upload
	 * @type String
	 ***/
	private $upload_save_path;


	/***
	 * Maximun upload size, in kilobytes
	 * @type Integer
	 ***/
	private $upload_max_size;


	/***
	 * The new name of the uploaded file
	 * @type String
	 ***/
	private $upload_file_name;


	/***
	 * Array containing the uploaded file's information
	 * @type Array
	 ***/
	private $upload_file_data;


	/***
	 * A list of permissable file types
	 * @type Array
	 ***/
	private $upload_allowable_extensions;


	/***
	 * Uploaded file's extension
	 * @type String
	 ***/
	public $upload_attrib_ext;


	/***
	 * Uploaded file's size
	 * @type Integer
	 ***/
	public $upload_attrib_size;


	/***
	 * Real name of the uploaded file
	 * @type String
	 ***/
	public $upload_attrib_name;


	/***
	 * Mime type of the uploaded file
	 * @type String
	 ***/
	public $upload_attrib_type;


	/***
	 * Temporary storage location of the uploaded file
	 * @type String
	 ***/
	public $upload_attrib_temp;


	// ! Constructor

	/***
	 * Instantiates object.
	 * @param none
	 ***/

	public function __construct()
	{
		$this->upload_save_path = '';
		$this->upload_max_size = 0;
		$this->upload_file_name = '';
		$this->upload_file_data = array();
		$this->upload_allowable_extensions = array();
		$this->upload_attrib_ext = '';
		$this->upload_attrib_size = 0;
		$this->upload_attrib_name = '';
		$this->upload_attrib_type = '';
		$this->upload_attrib_temp = '';
	}


   // ! Mutator

   /**
	* Sets the save path for the current uploaded file. Will
	* also check if the file is writable and if it actually
	* exists.
	*
	* @param $save_path Path to save uploads
	* @return Bool
	*/
	public function setUploadSavePath($save_path)
	{
		if(false == is_dir($save_path))
		{
			return false;
		}

		if(false == is_writable($save_path))
		{
			return false;
		}

		$this->upload_save_path = $save_path;

		return true;
	}


   // ! Mutator

   /**
	* Sets the maximum allowable upload size
	*
	* @param $max_size Size in kilobytes
	* @return true
	*/
	public function setUploadMaximumSize($max_size = 50)
	{
		$this->upload_max_size = (int) $max_size * 1028;

		return true;
	}


   // ! Mutator

   /**
	* Sets the current upload file's new name
	*
	* @param $name New name of file
	* @return true
	*/
	public function setNewFileName($name)
	{
		$this->upload_file_name = trim($name);

		return true;
	}


   // ! Mutator

   /**
	* Adds a new extension to the current stack. If the
	*
	* @param $reply_id The record id of the current reply
	* @return true
	*/
	public function addAllowableExtension($extension)
	{
		if(is_array($extension))
		{
			$this->upload_allowable_extensions = array_unique(array_merge($this->upload_allowable_extensions, $extension));

			return true;
		}

		$this->upload_allowable_extensions[] = $extension;

		return true;
	}


   // ! Mutator

   /**
	* Sets the current file upload info and populates all corresponding
	* attribute properties.
	*
	* @param $file_data An array containing all file data.
	* @return true
	*/
	public function setUploadFileData($file_data)
	{
		$this->upload_file_data = $file_data;

		$this->upload_attrib_ext = strtolower(end(explode ('.', $file_data['name'])));
		$this->upload_attrib_size = $file_data['size'];
		$this->upload_attrib_name = $file_data['name'];
		$this->upload_attrib_type = $file_data['type'];
		$this->upload_attrib_temp = $file_data['tmp_name'];

		return true;
	}


   // ! Executor

   /**
	* This method checks if the specified extension is permitted
	*
	* @param $extension The extension to validate
	* @return Bool
	*/
	public function isExtensionValid($extension)
	{
		if(false == $this->upload_allowable_extensions)
		{
			return true;
		}

		if(in_array($extension, $this->upload_allowable_extensions))
		{
			return true;
		}

		return false;
	}


   // ! Executor

   /**
	* Validates the current file's size
	*
	* @param $file_size An integer representing the size of the current file
	* @return Bool
	*/
	public function checkIfFileSizeIsValid($file_size)
	{
		if($this->upload_max_size && $this->upload_max_size < $file_size)
		{
			return false;
		}

		return true;
	}


   // ! Executor

   /**
	* This method will saves the current file to a
	* permanent location using the provided specifications
	*
	* @param none
	* @return Bool
	*/
	public function saveUpload()
	{
		return move_uploaded_file($this->upload_attrib_temp, $this->upload_save_path . DIRECTORY_SEPARATOR . ($this->upload_file_name ? $this->upload_file_name : $this->upload_attrib_name)))
	}
}

?>